{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "coastal-absolute",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/replace-words/\n",
    "\n",
    "\n",
    "Runtime: 472 ms, faster than 8.10% of C++ online submissions for Replace Words.\n",
    "Memory Usage: 217.7 MB, less than 18.10% of C++ online submissions for Replace Words.\n",
    "\n",
    "\n",
    "```cpp\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <string>\n",
    "#include <iostream>\n",
    "#include <iterator>\n",
    "#include <sstream>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "vector<string> split(const std::string& str, char delimiter = ' ')\n",
    "{\n",
    "    std::stringstream ss(str);\n",
    "    std::string token;\n",
    "    vector<string> r;\n",
    "    while (std::getline(ss, token, delimiter)) {\n",
    "        r.push_back(token);\n",
    "    }\n",
    "    return r;\n",
    "}\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    string replaceWords(vector<string>& dictionary, string sentence) {\n",
    "        string r = \"\";\n",
    "        bool ok;\n",
    "        sort(dictionary.begin(), dictionary.end());\n",
    "        for (auto word : split(sentence)) {\n",
    "            bool ok = false;\n",
    "            for (auto root : dictionary) {\n",
    "                if (root == word.substr(0, root.size())) {\n",
    "                    r += \" \" + root;\n",
    "                    ok = true;\n",
    "                    break;\n",
    "                }\n",
    "            }\n",
    "            if (ok == false) {\n",
    "                r += \" \" + word;\n",
    "            }\n",
    "        }\n",
    "        return r.substr(1, r.size()-1);\n",
    "    }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "piano-width",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
